using System;
using System.Collections.Generic;
using System.Text;

using PostSharp.Laos;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace EntlibAOP.ExceptionHandling
{
    [Serializable]
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class)]
    public class ExceptionHandlerAttribute : OnExceptionAspect
    {
        private string policyName;

        #region Properties
        /// <summary>
        /// Name of the exception policy to be associated
        /// </summary>
        public string PolicyName
        {
            get { return policyName; }
            set { policyName = value; }
        }
        #endregion

        #region Overloaded Methods
        /// <summary>
        /// This is the interceptor method that will implement the exception handling feature
        /// </summary>
        /// <param name="eventArgs">This contains the details of the exception occured</param>
        public override void OnException(MethodExecutionEventArgs eventArgs)
        {
            bool rethrow = ExceptionPolicy.HandleException
                   (eventArgs.Exception, policyName);
            if (rethrow)
                eventArgs.FlowBehavior = FlowBehavior.RethrowException;

        }
        #endregion
    }
}
